<?php  
## include required files
/*******************************/
require_once '../model/forums.php';
require_once '../model/users.php';
## Paginaton class

/*******************************/

## Create Objects
/*******************************/
$forumObj = new Model_Forums();
$userObj = new Model_Users();
/*******************************/

## Active, Inactive selected records
/*******************************/
$forum_id = base64_decode($_GET['forum_id']);
$smarty->assign('forum_id', $forum_id);

if(isset($_POST['go']) != '' && isset($_POST['action']) != '') {
		$ids = implode(",", $_POST['checkall']);
		## check which action is selected
		$hidden_page = $_POST['hidden_pageno'];	
		if($_POST['action'] == 'active') {
			## Active selected records
			$forumObj->updateForumCommentStatus($ids, '1');
			$_SESSION['msg']="<div class='success_msg'><span>Forum comment status activated successfully</span></div>";
		} else if($_POST['action'] == 'inactive') {
			## Inactive selected records
			$forumObj->updateForumCommentStatus($ids, '0');
			$_SESSION['msg']="<div class='success_msg'><span>Forum  comment status deactivated successfully</span></div>";
		} else {
			## Delete selected records
			$forumObj->deleteForumComment($ids);
			$_SESSION['msg']="<div class='success_msg'><span>Forum comment deleted successfully</span></div>";
		}
		header('location:'.SITE_URL.'/admin/home.php?q=forumcomments&forum_id='.$_GET['forum_id'].'&page='.$hidden_page);
		exit;
		## Delete code will be here if required
		
} // if close

## Active/Inactive/Delete thourgh URL
if(isset($_GET['action']) == 'status' && isset($_GET['status']) != '') {
	$id = base64_decode($_GET['id']);
	## Update forum Comment page status 
	$array = array();
	$array['status'] = ($_GET['status']=='1'?0:1);
	$forumObj->editForumCommentById($array, $id);
	$_SESSION['msg']="<div class='success_msg'><span>Forum comment status updated successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=forumcomments&forum_id='.$_GET['forum_id'].'&page='.$_GET['page']);
	exit;
}

if(isset($_GET['action']) == 'delete' && isset($_GET['id']) != '') {
	$id = base64_decode($_GET['id']);
	## Delete forum Comment
	$forumObj->deleteForumComment($id);
	$_SESSION['msg']="<div class='success_msg'><span>Forum comment deleted successfully</span></div>";
	header('location:'.SITE_URL.'/admin/home.php?q=forumcomments&forum_id='.$_GET['forum_id'].'&page='.$_GET['page']);
	exit;
}
/*******************************/

##Fetch all forum Comment pages from database
## Get search parameters in variables - 
if($_POST['search'] != '' && $_POST['search'] != 'Search') {
	$searchindex = $_POST['search'];
} else if($_GET['search'] != '') {
	$searchindex = $_GET['search'];
} else {
	$searchindex = '';
}
// Assign search variable to show in search textbox
$smarty->assign('search', $searchindex);

##  --------- Pagination part first start --------------##
	if( isset( $_GET[ 'page' ] ) && $_GET[ 'page' ] != "" ) {
		$pageNum = $_GET[ 'page' ];
	} else {
		$pageNum = 1;
	} 
	
	$rowsPerPage = ROW_PER_PAGE;
	$pageName = "home.php?q=forumcomments"; 
	$other_id = "search=".$searchindex;
	## Count all the records
	$forumCommentArray = $forumObj->getAllForumsComment($forum_id,$searchindex);
	$total_rows = count($forumCommentArray);
	$offset	= ($pageNum - 1) * $rowsPerPage;
	$smarty->assign('page', $pageNum);
##  --------- Pagination part first end --------------##


##  --------- Pagination part second start --------------##
	$newid = $pageNum * $rowsPerPage - $rowsPerPage + 1;
	$smarty->assign('newid',$newid);
	$forumCommentArray = $forumObj->getAllForumsComment($forum_id,$searchindex,$rowsPerPage,$offset);
	$i=0;
	foreach($forumCommentArray as $forumCommentArray_rs){
		$user_name = $userObj->getUserNameByUserId($forumCommentArray_rs['user_id']);  // GET THE COMMENT COUNT 
		$forumCommentArray[$i]['user_name'] = $user_name;
		$i++;
	}
	$smarty->assign('forumCommentArray',$forumCommentArray);
	
	if($searchindex != '') {
		$other_id = "search=".$searchindex;
	} else {
		$other_id = '';
	}
	
	if($total_rows > ROW_PER_PAGE) {
		$pg = new pagination();					// CREATING OBJECT OF THE PAGINATION CLASS
		$pgnation = $pg->pagination_admin( $total_rows , $rowsPerPage , $pageNum , $pageName , $other_id, $class);	
		$smarty-> assign("pagination",$pgnation);
	}
##  --------- Pagination part second end --------------##	

## Assign session message to smarty variable and unset session variable
if($_SESSION['msg'] != '') {
	$smarty->assign('msg', $_SESSION['msg']);	
	unset($_SESSION['msg']);
}

## Set active class variable for left menu  (main menu and sub menu)
$smarty->assign('activeclass', 'forums');
$smarty->assign('mainmenu', '3');

## Unset all the objects created which are on this page
unset($forumObj);
unset($userObj);

$smarty->display(TEMPLATEDIR_ADMIN. 'forums/forumcomments.tpl');
unset($smarty);
?>